package stack;

/**
 * Diese Klasse repräsentiert den Stack der SO-VM 
 */

import java.util.List;
import java.util.LinkedList;

public class Stack {

	/**
	 * Stack
	 */
	private List<Double> my_stack = null;

	/**
	 * Stack-Constructor
	 */
	public Stack() {
		this.my_stack = new LinkedList<Double>();
	}
	
	/**
	 * Copy-Constructor
	 */
	public Stack(Stack oldStack) {
		this.my_stack = new LinkedList<Double>();
		for (int i = 0; i<oldStack.getStack().size(); i++) {
			double h = oldStack.getStack().get(i);
			this.my_stack.add(h);
		}
	}

	/**
	 * Get the Stack
	 */
	public List<Double> getStack() {
		return this.my_stack;
	}
	
	/**
	 * Tests if the stack is empty
	 * 
	 * @return true, if the stack is empty
	 */
	public boolean isEmpty() {
		return (this.my_stack.size() == 0);
	}

	/**
	 * Returns the element on top of the stack without removing it from the stack
	 * 
	 * @return The element on the top of the stack
	 * @throws Underflow
	 */
	public double peek() throws Underflow {
		if (isEmpty()) {
			throw new Underflow();
		}
		return this.my_stack.get(this.my_stack.size() - 1);
	}

	/**
	 * Returns the element on the top of the stack and removes it from the stack
	 * 
	 * @return The element on the top of the stack
	 * @throws Underflow
	 */
	public double pop() throws Underflow {
		if (isEmpty()) {
			throw new Underflow();
		}
		return this.my_stack.remove(this.my_stack.size() - 1);
	}

	/**
	 * Pushes a double value on the top of the stack
	 * 
	 * @param o
	 */
	public void push(double o) {
		this.my_stack.add(o);
	}
	
	/**
	 * Pushes a integer value on the top of the stack
	 * 
	 * @param o
	 */
	public void push(int o) {
		this.my_stack.add((double) o);
	}
	
	/**
	 * Getter
	 * @return the size of the stack
	 */
	public int getSize() {
		return this.my_stack.size();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String result = "";
		for (int i = 0; i < this.my_stack.size(); i++) {
			result += this.my_stack.get(i)+" | ";
		}
		return result;
	}

}
